package net.maku.talent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.needy.vo.NeedyMemberVO;
import net.maku.talent.dao.HonorPersonDao;
import net.maku.talent.service.HonorPersonService;
import net.maku.talent.vo.HonorPersonVO;
import net.maku.talent.convert.HonorPersonConvert;
import net.maku.talent.entity.HonorPersonEntity;
import net.maku.talent.query.HonorPersonQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 个人荣誉
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-21
 */
@Service
@AllArgsConstructor
public class HonorPersonServiceImpl extends BaseServiceImpl<HonorPersonDao, HonorPersonEntity> implements HonorPersonService {

    private final HonorPersonDao honorPersonDao;

    @Override
    public PageResult<HonorPersonVO> page(HonorPersonQuery query) {
       //  IPage<HonorPersonEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        IPage<HonorPersonEntity> pageParams = getPage(query);
        QueryWrapper<HonorPersonVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getRealName()),"u.real_name",query.getRealName());
        queryWrapper.eq(query.getGender() != null,"u.gender",query.getGender());
        queryWrapper.eq(StringUtils.isNotBlank(query.getOrgName()),"o.name",query.getOrgName());
        queryWrapper.orderByDesc("nm.id");
        IPage<HonorPersonVO> page = honorPersonDao.selectPageList(pageParams,queryWrapper);
        return new PageResult<>(page.getRecords(),page.getTotal());
    }

    private LambdaQueryWrapper<HonorPersonEntity> getWrapper(HonorPersonQuery query){
        LambdaQueryWrapper<HonorPersonEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(HonorPersonVO vo) {
        HonorPersonEntity entity = HonorPersonConvert.INSTANCE.convert(vo);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
    }

    @Override
    public void update(HonorPersonVO vo) {
        HonorPersonEntity entity = HonorPersonConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}